package com.tfjybj.itoo.teach.provider.facade;

import org.apache.dubbo.config.annotation.Service;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.FacadeApi.RoomApi;
import com.tfjybj.itoo.teach.entity.RoomEntity;
import com.tfjybj.itoo.teach.model.RoomModel;
import com.tfjybj.itoo.teach.provider.service.RoomService;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author DUYU
 * @date 2019/5/22.
 */
@Service
@org.springframework.stereotype.Service
public class RoomFacade implements RoomApi {
    @Resource
    private RoomService roomService;

    /**
     * 添加
     *
     * @param model RoomModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult create(@RequestBody RoomModel model) {
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getRoomCapacity())) {
            return ItooResult.build(ItooResult.FAIL, "roomCapacity为空");
        }
        RoomEntity roomEntity = new RoomEntity();
        BeanUtils.copyProperties(model, roomEntity);
        roomService.save(roomEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult delete(String id) {
        roomService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult deleteByIds(List<String> ids) {
        roomService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model RoomModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult modify(RoomModel model) {
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getRoomCapacity())) {
            return ItooResult.build(ItooResult.FAIL, "roomCapacity为空");
        }
        RoomEntity roomEntity = new RoomEntity();
        BeanUtils.copyProperties(model, roomEntity);
        roomService.updateById(roomEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Room
     *
     * @param id 主键id
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult findById(String id) {
        RoomEntity roomEntity = roomService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomEntity);
    }

    /**
     * 分页查询所有Room
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult queryPageAll(Integer pageNo, Integer pageSize) {
        PageInfo<RoomEntity> rooms = roomService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", rooms);
    }

    /**
     * 根据房间代码模糊查询room
     *
     * @param code     房间代码
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的room
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult queryRoomByLikeCode(String code, Integer pageNo, Integer pageSize) {
        PageInfo<RoomEntity> roomList = roomService.queryByLikeCode(code, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据building_id查找所有房屋
     *
     * @param buildingId buildingId
     * @author 杨江涛
     * @since 2.0.0 2018-11-21 21:35:56
     */
    @Override
    public ItooResult findByBuildingId(String buildingId) {
        if (StringUtils.isEmpty(buildingId)) {
            return ItooResult.build(ItooResult.FAIL, "建筑ID为空");
        }
        try {
            List<RoomEntity> roomEntityList;
            roomEntityList = roomService.findByBuildingId(buildingId);
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomEntityList);
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "信息查询失败");
        }
    }

    /**
     * 根据roomId查找Room表所有信息
     *
     * @param roomId 主键roomId
     * @return roomEntityList
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult findByRoomId(String roomId) {
        List<RoomEntity> roomEntityList = roomService.findByRoomId(roomId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomEntityList);
    }

    /**
     * 根据校区代码查找Room表所有校区所有建筑的所有机房（返回list）
     *
     * @param schoolBranch 地理位置(010_西校区，011_东校区)
     * @return roomEntityList
     * @author 杨江涛
     * @since 2.0.0 2018-11-30 16:47:29
     */
    @Override
    public ItooResult findComputerRoom(String schoolBranch) {
        List<RoomEntity> roomEntityList = roomService.findComputerRoom(schoolBranch);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomEntityList);
    }

    /**
     * 根据roomId查找Room表所有信息
     *
     * @param pageNo     页码
     * @param pageSize   每页条数
     * @param searchInfo 查询参数
     * @return roomEntityList
     * @author 杨江涛
     * @since 2.0.0 2018-12-6 19:17:44
     */
    @Override
    public ItooResult findRoom(String searchInfo, Integer pageNo, Integer pageSize) {
        PageInfo<RoomEntity> result = roomService.findRoom(searchInfo, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", result);
    }


    /**
     * 根据校区号和开考时间筛选教师
     *
     * @param schoolCode 校区号
     * @param startTime  配置考试开始时间
     * @author 马莹
     * @since 2018-12-7 20:53:31
     */
    @Override
    public ItooResult queryNoExamRoom(String schoolCode, String startTime) {
        List<RoomEntity> roomEntityList = roomService.queryNoExamRoom(schoolCode, startTime);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomEntityList);
    }

    /**
     * 根据文本框中的值,模糊查询
     *
     * @param fuzzyValue 文本框中的值
     * @param schoolKey  校区号
     * @param startTime  考试开始时间
     * @param endTime    考试结束时间
     * @return 筛选的模糊值
     * @author 马莹
     * @since 2019-5-5 20:39:55
     */
    @Override
    public ItooResult selNotExamRoom(String fuzzyValue, String schoolKey, String startTime, String endTime) {
        try {
            List<RoomModel> roomModelList = roomService.selNotExamRoom(fuzzyValue, schoolKey, startTime, endTime);
            if (roomModelList.size() > 0) {
                return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomModelList);
            } else {
                return ItooResult.build(ItooResult.FAIL, "没有符合条件的考场");
            }
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "查询失败");
        }
    }

    /**
     * 根据东西校区以及考试开始时间加载考场
     *
     * @param schoolKey 校区key
     * @param startTime 考试开始时间
     * @param endTime   考试结束时间
     * @return 考场集合
     * @since 2019-3-24 17:33:12
     */
    @Override
    public ItooResult selNotRoom(String schoolKey, String startTime, String endTime) {
        try {
            List<RoomModel> roomModelList = roomService.selNotRoom(schoolKey, startTime, endTime);
            if (roomModelList.size() > 0) {
                return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomModelList);
            } else {
                return ItooResult.build(ItooResult.FAIL, "没有符合条件的考场");
            }
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "查询失败");
        }
    }
}
